package fr.umlv.m2.jee.pmlv.web.utils;

import fr.umlv.m2.jee.pmlv.service.user.User;
import fr.umlv.m2.jee.pmlv.service.user.impl.UserService;
import java.io.IOException;
import java.util.List;
import javax.naming.NamingException;
import javax.servlet.ServletContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


/**
 * Servlet wich get information about user and redirect to homepage of user.
 * @author Stephanie
 */
public class Login extends HttpServlet {

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
            super();
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response).
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request,response);
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response).
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        ServletContext context = getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        UserService userService = (UserService) applicationContext.getBean("ldapUser");
        User user = null;

        if(SecurityContextHolder.getContext()!=null)
        {
            if(SecurityContextHolder.getContext().getAuthentication()!=null)
            {
                //Save informations about user in session
                try {
                    user = userService.getUserDetails(SecurityContextHolder.getContext().getAuthentication().getName(), SecurityContextHolder.getContext().getAuthentication().getCredentials().toString());
                    session.setAttribute("user",user);
                } catch (NamingException ex) { }

                //Redirect to homepage of user
                GrantedAuthority[] authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
                for(int i=0;i<authorities.length;i++)
                {
                    if(authorities[i].getAuthority().equals("ROLE_STUDENT"))
                    {
                        try {
                            List<Integer> years = userService.getYearsByStudentId(user.getuId());
                            session.setAttribute("years", years);
                        } catch (NamingException ex) { }
                        response.sendRedirect(getServletContext().getContextPath()+"/Student");
                    }
                    else if(authorities[i].getAuthority().equals("ROLE_ADMIN"))
                        response.sendRedirect(getServletContext().getContextPath()+"/Admin");
                    else if(authorities[i].getAuthority().equals("ROLE_SECRETARY"))
                        response.sendRedirect(getServletContext().getContextPath()+"/Secretary");
                    else if(authorities[i].getAuthority().equals("ROLE_PROFESSOR"))
                    {
                        try {
                            List<Integer> years = userService.getYearsByProfessorId(user.getuId());
                            session.setAttribute("years", years);
                        } catch (NamingException ex) { }
                        response.sendRedirect(getServletContext().getContextPath()+"/Professor");
                    }
                }
            }
        }
    }
}
